package com.sx.core;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sx.article.entity.AttachmentEntity;
import com.sx.feesunit.exception.SXException;
import com.sx.rbac.entity.SysUserEntity;
import com.sx.util.SystemConfigInfo;
import com.sx.util.date.DateUtil;


@SuppressWarnings("serial")
public class CoreAction extends BaseAction {
	private File fileimg;
	private String fileimgFileName;
	private static final String CONTENT_TYPE = "text/html; charset=utf-8";
	private Log log = LogFactory.getLog(this.getClass());
//	public static final String ROOT = "back.war";
	public static final String ROOT = "front/attachment";
//	public static final String ROOTPATH = "/sx";
	/**
	 * 上传文件公共方法
	 * 返回上传后表单文件名称
	 **/
	public AttachmentEntity getUpdateFileInfo(String root,String displayname) throws SXException{
		/*SysUserEntity _user = super.getUserFromSession();
		System.out.println(_user);*/
		AttachmentEntity attachment = new AttachmentEntity();
		String _uploadpath = getRequest().getParameter("path");
		String _webrootpath = getServletContext().getContextPath();
		//获取网页的路径信息
		//获取物理路径信息
		String path = "";
		if (_uploadpath==null||("").equals(_uploadpath)) {
			path = "webattachment";
		} else {
			path = _uploadpath;
		}
		//文件上传
		//获取系统在计算机中的物理路径 如 C：\ncfams\webroot
		//E:\MyProject\PracticeProject\lsh\WebRoot\back//attachmentworkflow\2011-09-05day\
		String rootPath = getSession().getServletContext().getRealPath("/"); 
		rootPath += root+"\\"+path+"\\";  
	    //根据配置的标识来创建日期文件夹 重属性文件中读取叫 dateflag 的属性
		String day = SystemConfigInfo.ARTICLE_FILE_FOLDER_CREATETIME;
		//用来创建文件夹的名称
		String sp = rootPath + DateUtil.getDateName(day)+day+"//";
		//根据给定的名称 ”sp“ 创建文件夹
		MyUtils.mkDirectory(sp);
	    //获取网页中的web路径
		path = path.substring(0,path.lastIndexOf("\\"));
		String webcontentpath = _webrootpath+"/"+root+"/"+path+"/"+DateUtil.getDateName(day)+day;
		//开始进行上传的操作
        String filename = MyUtils.upload(this.getFileimgFileName().replaceAll(" ", ""), sp, this.getFileimg());  
        //上传完成后获取文件网站全目录信息
        String wholefilename = webcontentpath+"/"+filename;
        attachment.setFvaluename(filename);
        attachment.setFvaluepath(webcontentpath);
        attachment.setFdisplayname(displayname);
		return attachment;
	}
	/**
	 * 上传文件公共方法
	 * 返回上传后表单文件名称
	 **/
	public AttachmentEntity getUpdateFileInfo() throws SXException{
		AttachmentEntity attachment = new AttachmentEntity();
		String _uploadpath = getRequest().getParameter("path");
		String _webrootpath = getServletContext().getContextPath();
		//获取网页的路径信息
		//获取物理路径信息
		String path = "";
		if (_uploadpath==null||("").equals(_uploadpath)) {
			path = "webattachment";
		} else {
			path = _uploadpath;
		}
		//文件上传
		//获取系统在计算机中的物理路径 如 C：\ncfams\webroot
		//E:\MyProject\PracticeProject\lsh\WebRoot\back//attachmentworkflow\2011-09-05day\
		String rootPath = getSession().getServletContext().getRealPath("/"); 
		rootPath += ROOT+"\\"+path+"\\";  
	    //根据配置的标识来创建日期文件夹 重属性文件中读取叫 dateflag 的属性
		String day = SystemConfigInfo.ARTICLE_FILE_FOLDER_CREATETIME;
		//用来创建文件夹的名称
		String sp = rootPath + DateUtil.getDateName(day)+day+"//";
		//根据给定的名称 ”sp“ 创建文件夹
		MyUtils.mkDirectory(sp);
	    //获取网页中的web路径
		String webcontentpath = _webrootpath+"/"+ROOT+"/"+path+"/"+DateUtil.getDateName(day)+day;
		//开始进行上传的操作
        String filename = MyUtils.upload(this.getFileimgFileName().replaceAll(" ", ""), sp, this.getFileimg());  
        //上传完成后获取文件网站全目录信息
        String wholefilename = webcontentpath+"/"+filename;
        attachment.setFvaluename(filename);
        attachment.setFvaluepath(webcontentpath);
        attachment.setFdisplayname(fileimgFileName);
		return attachment;
	}
	/**
	 *上传图片文件 
	 **/
	public void uploadFileImage() throws SXException{
		try {
			String _uploadpath = getRequest().getParameter("path");
			String _webrootpath = getServletContext().getContextPath();
			//获取网页的路径信息
			//获取物理路径信息
			String path = "";
			if (_uploadpath==null||("").equals(_uploadpath)) {
				path = "webattachment";
			} else {
				path = _uploadpath;
			}
			//文件上传
			//获取系统在计算机中的物理路径 如 C：\ncfams\webroot
			//E:\MyProject\PracticeProject\lsh\WebRoot\back//attachmentworkflow\2011-09-05day\
			String rootPath = getSession().getServletContext().getRealPath("/"); 
			rootPath += ROOT+"\\"+path+"\\";  
		    //根据配置的标识来创建日期文件夹 重属性文件中读取叫 dateflag 的属性
			String day = SystemConfigInfo.ARTICLE_FILE_FOLDER_CREATETIME;
			//用来创建文件夹的名称
			String sp = rootPath + DateUtil.getDateName(day)+day+"//";
			//根据给定的名称 ”sp“ 创建文件夹
			MyUtils.mkDirectory(sp);
		    //获取网页中的web路径
			String webcontentpath = _webrootpath+"/"+ROOT+"/"+path+"/"+DateUtil.getDateName(day)+day;
			//开始进行上传的操作
	        String filename = MyUtils.upload(this.getFileimgFileName().replaceAll(" ", ""), sp, this.getFileimg());  
	        //上传完成后获取文件网站全目录信息
	        String wholefilename = webcontentpath+"/"+filename;
	        outJsonString("{success:true,filepath:'"+wholefilename+"'}");
		} catch (Exception e) {
			outJsonString("{success:false}");
			log.error("上传表单附件信息",e);
		}
	}
	/**
	 * 文件上传
	 **/
	public void uploadFileInfo() throws SXException{
		System.out.println("======>"+fileimgFileName);
		outJsonString("{success:true}");
	}
	/**
	 *设置加载页面共享目录数据信息 
	 **/
	public String includeLibs(){
		String page = "includelibs";
		try {
			page = "includelibs";
		} catch (Exception e) {
			log.error("设置共享目录数据信息!",e);
		}
		return page ;
	}
	/**
	 * @return the fileimg
	 */
	public File getFileimg() {
		return fileimg;
	}
	/**
	 * @param fileimg the fileimg to set
	 */
	public void setFileimg(File fileimg) {
		this.fileimg = fileimg;
	}
	/**
	 * @return the fileimgFileName
	 */
	public String getFileimgFileName() {
		return fileimgFileName;
	}
	/**
	 * @param fileimgFileName the fileimgFileName to set
	 */
	public void setFileimgFileName(String fileimgFileName) {
		this.fileimgFileName = fileimgFileName;
	}
	
}
